package com.rongzhe.house.presenter;

import android.content.Intent;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.HouseCriteriaTo;
import com.rongzhe.house.entity.voo.HouseOverview;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.ui.FilterChangeListener;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.RentTypeActivity;

import java.util.List;

/**
 * Created by huang on 2017/12/5.
 */

public class RentTypePresenter extends BasePresenter implements FilterChangeListener{
    private HouseCriteriaTo mHouseCriteriaTo = new HouseCriteriaTo();
    private PageContoller<HouseOverview> mPageContoller;

    public RentTypePresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void start(Intent intent) {
        super.start(intent);
        mPageContoller = new PageContoller<>((RentTypeActivity)getUiControlInterface());
        String type = intent.getStringExtra(RentTypeActivity.EXTRA_TITLE);

        if (getString(R.string.rent_together).equals(type)) {
            mHouseCriteriaTo.setAsEntireRent(false);
        } else if (getString(R.string.rent_all).equals(type)) {
            mHouseCriteriaTo.setAsEntireRent(true);
        } else if (getString(R.string.rent_direct).equals(type)) {
            mHouseCriteriaTo.setAsOwnerRent(true);
        } else if (getString(R.string.rent_apartment).equals(type)) {
            mHouseCriteriaTo.setAsApartMent(true);
        }
        loadData(false);
    }

    @Override
    public void onFilterChanged() {
        loadData(false);
    }

    public void loadData(final boolean append) {
        HouseManager.getInstance().queryHouse(mPageContoller.getMaxDataOnePage(),
                mPageContoller.getCurrentPage(),
                new DataListener<List<HouseOverview>>() {
                    @Override
                    public void onSuccess(List<HouseOverview> data, String message) {
                        mPageContoller.onDataLoaded(data, append);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                        getUiControlInterface().showToast(message);
                    }

                    @Override
                    public void onEnd() {
                        mPageContoller.onDataLoadFinish(append);
                    }
                }, mHouseCriteriaTo);
    }
}
